import logging  
from twisted.internet import defer  
from twisted.internet import reactor  
from twisted.web.client import getPage  

class pageunit:
	def __init__(self):
		self.log = logging.getLogger('retry')
	
	def retry(self, times, func, *args, **kwargs): 
		"""retry a defer function
		@param times: how many times to retry
		@param func: defer function
		"""
		errorList = []
		deferred = defer.Deferred()
		def run():
			self.log.info('Try %s(*%s, **%s)', func.__name__, args, kwargs)
			d = func(*args, **kwargs) 
			d.addCallbacks(deferred.callback, error)
		def error(error):  
			errorList.append(error)
			# Retry 
			if len(errorList) < times: 
				self.log.info('Failed to try %s(*%s, **%s) %d times, retry...', \
						func.__name__, args, kwargs, len(errorList))  
			        run() 
			# Fail  
			else:
				self.log.info('Failed to try %s(*%s, **%s) over %d times, stop', \
						func.__name__, args, kwargs, len(errorList))  
				deferred.errback(errorList)  
		run()
		return deferred

	def output(self, data):
		print 'output', unicode(data, 'big5')
		reactor.stop()

	def error(self, error):
		print 'final error', error
		reactor.stop()

	def getPage(self, URL):
		d = self.retry(3, getPage, 'http://mis.tse.com.tw/data/%s.csv'%URL)  
		d.addCallbacks(self.output, self.error)  
		reactor.run()

